using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

namespace Behemoth.SharePoint.Administration {

    [DefaultProperty("WebAppSelector")]
    [ToolboxData("<{0}:WebModificationsGrid runat=server></{0}:WebModificationsGrid>")]
    public class WebModificationsGrid : WebControl {

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            this.Page.ClientScript.RegisterClientScriptInclude("WebModificationsGrid", "/_layouts/WebConfigModificationAdmin.js");
        }

        [Bindable(true)]
        [Category("Appearance")]
        [Localizable(false)]
        public WebApplicationSelector WebAppSelector {
            get { return selector; }
            set { selector = value; }
        }
        protected WebApplicationSelector selector;

        private Collection<SPWebConfigModification> GetWebApplicationModifications() {
            if (selector == null)
                return null;
            return selector.CurrentItem.WebConfigModifications;
        }

        // TODO use a grid control... this stuff is kludgy
        protected override void RenderContents(HtmlTextWriter output) {
            if (selector == null) {
                // TODO use an error bag for uniform look and feel
                output.Write("<div class=\"ErrorMessage\">The control has not been bound to a SharePoint application selector.</div>");
                return;
            }
            output.Write("<table border=\"0\" cellpadding=\"0\" width=\"100%\" id=\"idResults1\" class=\"ms-listviewtable\" style=\"border-collapse:collapse\">");
            output.Write("<tr class=\"ms-viewheadertr\">");
            output.Write("<th class=\"ms-vh2-nofilter ms-vh2-gridview\" style=\"width:20px\">&nbsp;</th>");
            output.Write("<th class=\"ms-vh2-nofilter ms-vh2-gridview\">Type</th>");
            output.Write("<th class=\"ms-vh2-nofilter ms-vh2-gridview\">Owner</th>");
            output.Write("<th class=\"ms-vh2-nofilter ms-vh2-gridview\">Path</th>");
            output.Write("<th class=\"ms-vh2-nofilter ms-vh2-gridview\">Name</th>");
            output.Write("<th class=\"ms-vh2-nofilter ms-vh2-gridview\">Value</th>");
            output.Write("<th class=\"ms-vh2-nofilter ms-vh2-gridview\">Sequence</th>");
            output.Write("</tr>");

            Hashtable ActiveWebConfigModifications = new Hashtable();
            Collection<SPWebConfigModification> mods = GetWebApplicationModifications();
            foreach (SPWebConfigModification m in mods) {              
              string sKey = String.Format("{0}_{1}", m.Path, m.Name);
              object modExists = ActiveWebConfigModifications[sKey];

              if(modExists==null) {
                  ActiveWebConfigModifications.Add(sKey, m);
              } 
              else {
                if (m.Sequence >= ((SPWebConfigModification) modExists).Sequence)  {
                  ActiveWebConfigModifications.Remove(sKey);
                  ActiveWebConfigModifications.Add(sKey, m);
                }
              }
            }

            int c = 0;
            foreach (SPWebConfigModification m in mods) {
                string ActiveModification = "inactive-modification";
                SPWebConfigModification mh = (SPWebConfigModification) ActiveWebConfigModifications[String.Format("{0}_{1}", m.Path, m.Name)];
                if (m.Equals(mh) && m.Sequence == mh.Sequence) {
                    ActiveModification = "active-modification";
                }

                output.Write("<tr class=\"{0} {1}\">", Alternate(c), ActiveModification);
                output.Write("<td class=\"ms-vb2\"><input type=\"checkbox\" ID=\"checkedItem{0}\" name=\"checkedItem{0}\" /> </td>", c.ToString());
                output.Write("<td class=\"ms-vb2\">{0}</td>", m.Type);
                output.Write("<td class=\"ms-vb2\">{0}</td>", m.Owner);
                output.Write("<td class=\"ms-vb2\">{0}</td>", m.Path);
                output.Write("<td class=\"ms-vb2\">{0}</td>", m.Name);
                output.Write("<td class=\"ms-vb2\"><input type=\"text\" ID=\"updateItem{0}\" name=\"updateItem{0}\" value=\"{1}\" OnMouseUp=\"ValueChanged(this, {0});\" OnKeyUp=\"ValueChanged(this, {0});\" class=\"ms-input\" /></td>", c.ToString(), HttpUtility.HtmlEncode(m.Value));
                output.Write("<td class=\"ms-vb2\">{0}</td>", m.Sequence);
                output.Write("<input type=\"hidden\" value=\"{1}\" ID=\"valueItem{0}\" name=\"valueItem{0}\" />", c.ToString(), HttpUtility.HtmlEncode(m.Value));
                output.Write("</tr>");
                c++;                
            } // for 
            output.Write("</table>");
        } // fn render

        protected string Alternate(int c) {
            return (c % 2) > 0 ? "ms-alternating" : "";
        }

    } // class
} // namespace
